<?php
/**
 * Created by PhpStorm.
 * User: wangan
 * Date: 2017/2/24
 * Time: 10:18
 */

namespace App\Http\Controllers\Admin;




use App\Models\UserViewitems;
use App\Models\ViewItems;
use Illuminate\Http\Request;

class ViewItemsController extends BaseController
{
    public function index(Request $request){
        $int_permissionId = $request->get('permissionId');
        $int_pageSize = $request->get("pageSize",20);
        $int_page = $request->get('page',1);
        $obj_viewItems = ViewItems::where('permission_id',$int_permissionId)
                            ->orderBy('id','DESC')
                            ->paginate($int_pageSize);
        return $this->view('view-items.index',[
            'pageSize'=>$int_pageSize,
            'viewItems'=>$obj_viewItems,
            'permissionId'=>$int_permissionId,
        ]);
    }

    public function create(Request $request){
        $int_permissionId = $request->get('permissionId');
        return $this->view('view-items.create',[
            'permissionId' => $int_permissionId
        ]);
    }

    public function doCreate(Request $request){
        $array_info = $request->get('info');
        $this->itemsValidate($request);

        if((ViewItems::create($array_info))){
            return redirect('/admin/viewItems/index?permissionId='.$array_info['permission_id']);
        }else{
            return redirect()->back();
        }
    }

    public function edit(Request $request){
        $int_itemId = $request->get('itemsId');
        $obj_items = ViewItems::find($int_itemId);
        return $this->view('view-items.edit',[
            'viewItems'=>$obj_items,
        ]);
    }

    public function doEdit(Request $request){
        $array_viewItems = $request->get('info');
        $this->itemsValidate($request);
        if(ViewItems::where('id',$array_viewItems['id'])->update($array_viewItems)){
            return redirect('/admin/viewItems/index?permissionId='.$array_viewItems['permission_id']);
        }else{
            return redirect()->back();
        }
    }

    public function getItemsByPermissions(Request $request){
        $array_permissions = $request->get('permissions');
        $array_items = ViewItems::whereIn('permission_id',$array_permissions)->get()->toArray();
        return $this->output('1','',[
            'list' =>$array_items,
        ]);
    }


    public function delete(Request $request){
        $int_id = $request->get('id');
        if(ViewItems::find($int_id)->delete()){
            return  $this->output('1','删除成功！',[]);
        }
        return  $this->output('0','删除！',[]);
    }

    public function getUserCanViewItems(Request $request){
        $int_userId = $request->get('userId');
        $obj_viewItems = UserViewitems::select(['view_items.id as id',
                        'view_items.name as name',
                        'view_items.permission_id as permission_id',
                        'view_items.description as description',])
                        ->leftJoin('view_items', 'view_items.id', '=', 'user_viewitems.viewitems_id')
                        ->where('user_viewitems.user_id', $int_userId)
                        ->get();
        $array_viewItems = $obj_viewItems->toArray();
        return $this->output('1','',[
            'list'=>$array_viewItems
        ]);
    }

    private function itemsValidate(Request $requset){
        $this->validate($requset,[
            'info.name'=>'required|alpha_dash',
            'info.description'=>'required',
        ],[
            'required'=>':attribute不能为空',
            'integer'=>':attrbute必须为整数',
            'alpha_num'=>':attrbute必须为字母或数字'
        ],[
            'info.name'=>'元素名称',
            'info.description'=>'描述',
        ]);
    }

}